import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import { Message } from "element-ui"
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router'
import router from "./router"
import store from "./Stores/store"
import Cookies from "js.cookie";
import userApi from "./services/user.js";
Vue.config.productionTip = false

import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: 'QPhdiyeQ5MnqISjaEYdQoYYKdmMYhVKU'
})

Vue.use(VueRouter)
Vue.use(ElementUI)



router.beforeEach((to, from, next) => {
  if (to.matched.some(m => m.meta.auth)) {
    // 对路由进行验证
    if (store.state.permission.isLogin == true) { // 已经登陆
      next() // 正常跳转到你设置好的页面
    } else {
      // 未登录则跳转到登陆界面，query:{ Rurl: to.fullPath}表示把当前路由信息传递过去方便登录后跳转回来；
      next({ name: "登陆", query: { Rurl: to.fullPath } })
    }
  } else {
    next()
  }
})
async function init() {
  //根据当前是否有cookie，设置isLogin的值
  if (Cookies.get("userId")) {
    const res = await userApi.whoami();
    store.commit("permission/setLoginUser", res.data);
  } else {
    Message({
      type: "error",
      message: "请先登陆！获得权限"
    });
  }
  new Vue({
    router,
    store,
    render: h => h(App),
  }).$mount('#app')
}
init()
